package com.qtummatrix.dao.impl;

import com.qtummatrix.dao.SaveMoudleDao;
import com.qtummatrix.entity.*;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
 * create by Gongshiyong  2019-11-21 19:39
 */

@Repository("saveMoudleDao")
public class SaveMoudleDaoImpl implements SaveMoudleDao {
    @Resource
    private SessionFactory sessionFactory;

    private Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public void addQuestions(QuestionsEntity questions) {
        Session session=getSession();
        session.saveOrUpdate(questions);
        session.flush();
        session.clear();

    }

    @Override
    public QuestionsEntity get_QuestionbyContent(String content) {
        String hql="from QuestionsEntity where content=?";
        Session session=getSession();
        Query query=session.createQuery(hql);
        query.setParameter(0,content);
        QuestionsEntity questionsEntity=(QuestionsEntity) query.uniqueResult();
        return questionsEntity;
    }

    @Override
    public int get_sumpage(int scopeid,int typeid,String content) {
        if (!content.equals("")){
            content="%"+content+"%";
        }
        String hql="";
        Session session=getSession();
        Query query = null;
        if (scopeid==0&&typeid!=0&&content.equals("")){
            hql="from QuestionsEntity where kindId=? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,typeid);
        }
        else if (scopeid!=0&&typeid==0&&content.equals("")){
            hql="from QuestionsEntity where scopeId=? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,scopeid);
        }
        else if (scopeid!=0&&typeid!=0&&content.equals("")){
            hql="from QuestionsEntity where scopeId=? and kindId=? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,scopeid);
            query.setParameter(1,typeid);
        }
        else if (scopeid==0&&typeid!=0&&!content.equals("")){
            hql="from QuestionsEntity where kindId=? and content like ? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,typeid);
            query.setParameter(1,content);
        }
        else if (scopeid!=0&&typeid==0&&!content.equals("")){
            hql="from QuestionsEntity where scopeId=? and content like ? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,scopeid);
            query.setParameter(1,content);
        }
        else if (scopeid!=0&&typeid!=0&&!content.equals("")){
            hql="from QuestionsEntity where kindId=? and scopeId=? and content like ? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,typeid);
            query.setParameter(1,scopeid);
            query.setParameter(2,content);
        }
        else if (scopeid==0&&typeid==0&&!content.equals("")){
            hql="from QuestionsEntity where content like ?";
            query=session.createQuery(hql);
            query.setParameter(0,content);
        }
        else {
            hql="from QuestionsEntity order by id desc";
            query=session.createQuery(hql);
        }
        List<QuestionsEntity> list=query.list();
        return list.size();
    }


    @Override
    public List<QuestionsEntity> get_Question(int scopeid, int typeid, String content, int n) {
        if (!content.equals("")){
            content="%"+content+"%";
        }
        String hql="";
        int page=n*11;
        Session session=getSession();
        Query query = null;
        if (scopeid==0&&typeid!=0&&content.equals("")){
            hql="from QuestionsEntity where kindId=? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,typeid);
        }
        else if (scopeid!=0&&typeid==0&&content.equals("")){
            hql="from QuestionsEntity where scopeId=? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,scopeid);
        }
        else if (scopeid!=0&&typeid!=0&&content.equals("")){
            hql="from QuestionsEntity where scopeId=? and kindId=? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,scopeid);
            query.setParameter(1,typeid);
        }
        else if (scopeid==0&&typeid!=0&&!content.equals("")){
            hql="from QuestionsEntity where kindId=? and content like ? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,typeid);
            query.setParameter(1,content);
        }
        else if (scopeid!=0&&typeid==0&&!content.equals("")){
            hql="from QuestionsEntity where scopeId=? and content like ? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,scopeid);
            query.setParameter(1,content);
        }
        else if (scopeid!=0&&typeid!=0&&!content.equals("")){
            hql="from QuestionsEntity where kindId=? and scopeId=? and content like ? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,typeid);
            query.setParameter(1,scopeid);
            query.setParameter(2,content);
        }
        else if (scopeid==0&&typeid==0&&!content.equals("")){
            hql="from QuestionsEntity where content like ?";
            query=session.createQuery(hql);
            query.setParameter(0,content);
        }
         else {
            hql="from QuestionsEntity order by id desc";
            query=session.createQuery(hql);
        }
        query.setMaxResults(11);
        query.setFirstResult(page);
        List<QuestionsEntity> list=query.list();
        return list;
    }

    @Override
    public int get_MoudleSumpage(int scopeid,String name){
        String hql="";
        Session session=getSession();
        Query query=null;
        if (scopeid==0&&name.equals("")){
            hql="from MoudleEntity order by id desc";
            query=session.createQuery(hql);
        }
        else if (scopeid!=0&&name.equals("")){
            hql="from MoudleEntity where scopeId=? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,scopeid);
        }
        else if (scopeid==0&&!name.equals("")){
            hql="from MoudleEntity where moudlename like ? order by id desc";
            name="%"+name+"%";
            query=session.createQuery(hql);
            query.setParameter(0,name);
        }
        else {
            name="%"+name+"%";
            hql="from MoudleEntity where moudlename like ? and scopeId=? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,name);
            query.setParameter(1,scopeid);
        }

        List<MoudleEntity> list=query.list();
        return list.size();
    }

    @Override
    public List<MoudleEntity> get_Moudle(int scopeid , String name, int n) {

        String hql="";
        Session session=getSession();
        Query query=null;
        int page=n*11;
        if (scopeid==0&&name.equals("")){
            hql="from MoudleEntity order by id desc";
            query=session.createQuery(hql);
        }
        else if (scopeid!=0&&name.equals("")){
            hql="from MoudleEntity where scopeId=? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,scopeid);
        }
        else if (scopeid==0&&!name.equals("")){
            hql="from MoudleEntity where moudlename like ? order by id desc";
            name="%"+name+"%";
            query=session.createQuery(hql);
            query.setParameter(0,name);
        }
        else {
            name="%"+name+"%";
            hql="from MoudleEntity where moudlename like ? and scopeId=? order by id desc";
            query=session.createQuery(hql);
            query.setParameter(0,name);
            query.setParameter(1,scopeid);
        }

        query.setMaxResults(11);
        query.setFirstResult(page);

        List<MoudleEntity> list=query.list();
        return list;
    }

    @Override
    public List<Integer> get_QuestionIDByMoudleID(int moudleID) {
        String hql="select issueId from MoudleQuestionEntity where moudleId=?";
        Session session=getSession();
        Query query=session.createQuery(hql);
        query.setParameter(0,moudleID);
        List<Integer> list=query.list();
        return list;
    }

    @Override
    public List<QuestionsEntity> get_QuestionByID(List<Integer> idlist) {
            String hql="from QuestionsEntity where id in (:list)";
            Session session=getSession();
            Query query=session.createQuery(hql);
            query.setParameterList("list",idlist);
            List<QuestionsEntity> list=query.list();

        return list;
    }

    @Override
    public void del_questionsfromMoudel(int id) {
            String hql="delete MoudleQuestionEntity where issueId=?";
            Session session=getSession();
            Query query=session.createQuery(hql);
            query.setParameter(0,id);
            query.executeUpdate();
    }

    @Override
    public List<QuestionsEntity> get_questionsByScope(int scopeId) {
          String hql="from QuestionsEntity where scopeId=?";
          Session session=getSession();
          Query query=session.createQuery(hql);
          query.setParameter(0,scopeId);
          List<QuestionsEntity> list=query.list();
          return list;
    }

    @Override
    public boolean add_Moudle(MoudleEntity moudleEntity) {

        Session session=getSession();
        if(moudleEntity.getId()==0){
            String hql="from MoudleEntity where moudlename=?";
            Query query=session.createQuery(hql);
            query.setParameter(0,moudleEntity.getMoudlename());
            if (query.uniqueResult()!=null){
                return false;
            }
        }

        session.saveOrUpdate(moudleEntity);
        return true;
    }
    @Override
    public int get_moudleid(String name){
        Session session=getSession();
        String hql="select id from MoudleEntity where moudlename=?";

        Query query=session.createQuery(hql);
        query.setParameter(0,name);
        int id=(int)query.uniqueResult();
        return id;
    }

    @Override
    public void del_moudle(int moudleid) {
            String hql="delete from MoudleEntity where id=?";
            Session session=getSession();
            Query query=session.createQuery(hql);
            query.setParameter(0,moudleid);
            query.executeUpdate();

            hql="delete from MoudleQuestionEntity where moudleId=?";
            query=session.createQuery(hql);
            query.setParameter(0,moudleid);
            query.executeUpdate();

            hql="delete from ModelMoudle where moudleid=?";
            query=session.createQuery(hql);
            query.setParameter(0,moudleid);
            query.executeUpdate();
    }

    @Override
    public List<ModelEntity> get_goodsMoudleInfo() {

        String hql ="from ModelEntity";
        Session session=getSession();
        Query query=session.createQuery(hql);
        List<ModelEntity> list=query.list();

        return list;
    }

    @Override
    public List<ModelMoudle> getModelMoudle() {

        String hql="from ModelMoudle";
        Session session=getSession();
        Query query=session.createQuery(hql);
        List<ModelMoudle> list=query.list();
        return list;
    }

    @Override
    public List<String> getQuestionMoudleByID(List<Integer> list) {
        String hql="select moudlename from MoudleEntity where id=?";

        Session session=getSession();
        Query query=session.createQuery(hql);
        List<String> stringList=new ArrayList<>();
        for (int i=0;i<list.size();i++) {
            query.setParameter(0, list.get(i));
            stringList.add((String) query.uniqueResult());
        }
        return stringList;
    }

    @Override
    public List<MoudleEntity> getAllmoudleEntity() {
        String hql="from MoudleEntity";

        Session session=getSession();

        Query query=session.createQuery(hql);

        List<MoudleEntity> list=query.list();

        return list;
    }

    @Override
    public void updateGoodsMoudle(int id,int moudleid) {
        String hql="from ModelMoudle where modelid=?";
        Session session=getSession();
        Query query=session.createQuery(hql);
        query.setParameter(0,id);
        ModelMoudle modelMoudle=(ModelMoudle) query.uniqueResult();
        if (modelMoudle==null){
            ModelMoudle modelMoudle1=new ModelMoudle();
            modelMoudle1.setMoudleid(moudleid);
            modelMoudle1.setModelid(id);
            session.save(modelMoudle1);
            return;
        }
        modelMoudle.setMoudleid(moudleid);
        session.saveOrUpdate(modelMoudle);
    }


    @Override
    public void add_questionsMoudle(int moudleid, int[] questionsid) {
        List<MoudleQuestionEntity> list=new ArrayList<>();
        String hql="delete from MoudleQuestionEntity where moudleId=?";
        Session session=getSession();
        Query query=session.createQuery(hql);
        query.setParameter(0,moudleid);
        query.executeUpdate();
        for (int i=0;i<questionsid.length;i++){
            MoudleQuestionEntity moudleQuestionEntity=new MoudleQuestionEntity();
            moudleQuestionEntity.setMoudleId(moudleid);
            moudleQuestionEntity.setIssueId(questionsid[i]);
            list.add(moudleQuestionEntity);
        }
        for (int i=0;i<list.size();i++){
            session.save(list.get(i));
            session.flush();
            session.clear();
        }

    }

    @Override
    public void del_questionsfromBank(int id) {
            String hql="delete QuestionsEntity where id=?";
            Session session=getSession();
            Query query=session.createQuery(hql);
            query.setParameter(0,id);
            query.executeUpdate();
    }

    @Override
    public int get_ScopeID(String scopeName) {
        String hql="select id from ScopeEntity where scopeName=?";
        Session session=getSession();
        Query query=session.createQuery(hql);
        query.setParameter(1,scopeName);
        int id=(int)query.uniqueResult();
        return id;
    }

    @Override
    public int get_TypeID(String typeName) {
        String hql="select id from QuestkindEntity where questName=?";
        Session session=getSession();
        Query query=session.createQuery(hql);
        query.setParameter(1,typeName);
        int id=(int)query.uniqueResult();
        return id;
    }


}
